package com.lvtongka.app.ui.card

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.lvtongka.app.base.BaseViewModel
import com.lvtongka.app.http.HttpCallBack
import com.lvtongka.app.http.OkhttpUtils
import com.lvtongka.app.http.common.UrlConstants
import com.lvtongka.app.ui.card.bean.BindCardRequest
import com.lvtongka.app.ui.card.bean.CardBagData
import com.lvtongka.app.ui.card.bean.CardBagResponse
import com.lvtongka.app.ui.card.bean.CardData
import com.lvtongka.app.ui.card.bean.CardRequest
import com.lvtongka.app.ui.card.bean.CardResponse
import com.lvtongka.app.ui.card.bean.UnBindCardRequest
import com.lvtongka.app.ui.recharge.bean.OcrData
import com.lvtongka.app.ui.recharge.bean.OcrRequest
import com.lvtongka.app.ui.recharge.bean.OcrResponse

/**
 * describe:
 *
 * @author:ZhangWei(张伟)
 * date:2024/11/9
 * time:16:53
 */
class BindCardViewModel : BaseViewModel() {

    private val _cardData = MutableLiveData<CardData>()
    val cardData: LiveData<CardData> = _cardData

    private val _bindCardBagData = MutableLiveData<CardBagData>()
    val bindCardBagData: LiveData<CardBagData> = _bindCardBagData

    private val _unBindCardBagData = MutableLiveData<CardBagData>()
    val unBindCardBagData: LiveData<CardBagData> = _unBindCardBagData

    private val _ocrData = MutableLiveData<OcrData>()
    val ocrData: LiveData<OcrData> = _ocrData

    // ocr识别
    fun getOcrInfo(pictureBase64: String) {
        val request = OcrRequest(pictureBase64)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val ocrResponse = Gson().fromJson(result, OcrResponse::class.java)
                    _ocrData.postValue(ocrResponse.data)
                }
            }, getAccessToken())
    }

    // 读取旅通卡信息
    fun getCardInfo(ltkCard: String, ltkPwd: String) {
        val request = CardRequest(ltkCard, ltkPwd)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val recharge = Gson().fromJson(result, CardResponse::class.java)
                    _cardData.postValue(recharge.data)
                }
            }, getAccessToken())
    }

    // 绑定旅通卡
    fun bindCard(ltkCard: String, ltkPwd: String) {
        val request = BindCardRequest(ltkCard, ltkPwd)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val resp = Gson().fromJson(result, CardBagResponse::class.java)
                    _bindCardBagData.postValue(resp.data)
                }
            }, getAccessToken())
    }

    // 解绑定旅通卡
    fun unBindCard(ltkCard: String) {
        val request = UnBindCardRequest(ltkCard)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val resp = Gson().fromJson(result, CardBagResponse::class.java)
                    _unBindCardBagData.postValue(resp.data)
                }
            }, getAccessToken())
    }
}